<template>
  <div>
    <div class="user_info_div">
      <div class="head_big">
        <x-img
          :src="userPo.headImg"
          :default-src="userPo.defaultheadImg"
          height="80"
        />
      </div>
    <div class="user_name">{{userPo.name}}</div>
      <div class="user_detail">{{userPo.schoolName}} - {{userPo.departmentName}} - {{userPo.majorName}}</div>
    </div>

    <group label-width="4.5em" class="info_div" v-if="!isOpenView" label-margin-right="2em" label-align="right">
      <cell title="学籍信息" value="修改"  @click.native="toPassword" is-link></cell>
    </group>
    
    <div class="qiehuan_div" v-if="isShowChange  && !isOpenView">
             <x-button type="warn"  @click.native="doTeaRole" class="dskt_btn">切换到开单角色</x-button>
    </div>

    <div class="buttom_div" v-if="isDebug">
              <x-button type="primary" id="tuichu_btn" @click.native="doOut" class="dskt_btn">退出登录</x-button>
    </div>

  </div>
</template>

<script>
import { XImg, Group, Cell } from "vux";
import dsktConstant from "../../config/constant";
import userService from '../../shared/service/userService'
import weixinService from "../../shared/service/weixinService";
const wx = require('weixin-js-sdk')
export default {
  components: {
    XImg,
    Group,
    Cell
  },
  data() {
    var data={
       userPo :  this.$cookie.getLoginUserData(),
    }
    this.dealUserPo(data.userPo);
    data.isShowChange = false;
    data.isOpenView = this.$route.name=='stu_open_center';
    data.isDebug = dsktConstant.debug;
    data.staffNo=null;
    data.realStuUserFileId =  this.$cookie.get( this.$cookie.key.openOrderStuFileId);
    data.realStuUserId =  this.$cookie.get( this.$cookie.key.openOrderStuUserId);
    return data;
  },
  mounted() {
     weixinService.initWx(wx);
    //页面滚动给顶部的搜索栏添加一个样式.
    this.getData();
    this.getStuData( this.realStuUserFileId );
    //如果不一样 把它换成学生的
    if(this.$cookie.get(this.$cookie.key.NOWTYPE)){
      if( 'STUDENT' !=  this.$cookie.get(this.$cookie.key.NOWTYPE) ){
        var vm =this;
        this.$vux.alert.show({
          title: '提示',
          content: '角色错误',
          onHide () {
            vm.$vux.loading.show();
            userService.getStuById( vm.realStuUserId).then((res)=>{
              vm.loginOk(res.data);
              vm.$cookie.set(vm.$cookie.key.NOWTYPE,'STUDENT');
              vm.$vux.loading.hide();
              vm.$vux.toast.text("已切换回学生");
            });
          }
        });
      }
    }
  },
  methods: {
    dealUserPo(userPo){
        if(userPo.headImg){
            userPo.defaultheadImg=userPo.headImg;
          }else{
              userPo.defaultheadImg='./static/img/avatar_default.png';
          }
    },
    //退出登录
    toPassword(){
      this.$router.push({ name: "student_info", params: { id: "1" } });
    },
    //切换到教师
    doTeaRole(){
      userService.getTeaByNo(this.staffNo).then((res)=>{
        this.loginOk(res.data);
        this.$cookie.set(this.$cookie.key.StaffNo,this.staffNo);
        this.$cookie.set(this.$cookie.key.NOWTYPE,'TEACHER');
        this.$router.push({ name: "user_center" });
      });
      
    },
     getStuData(stuFileId) {
       userService.getStudentInfo(stuFileId).then((res)=>{
         var po = res.data;
         this.dealUserPo(po);
          this.userPo=po;
      });
    },
    //copy 登录的
     loginOk(data) {
      var user = data;
      this.$cookie.set(this.$cookie.key.userId, user.id);
      if (user.staffFile) {
        var staffFile = user.staffFile;
        this.$cookie.setLoginUserData(staffFile);
      }else if (user.studentFile) {
        var studentFile = user.studentFile;
        this.$cookie.setLoginUserData(studentFile,studentFile.id);
      }
    },
    getData() {
       userService.getStudentOpenOrder(this.realStuUserId).then((res)=>{
         this.staffNo=res.data.staffFile.no;
         this.isShowChange=true;
      }).catch(error=>{
         console.log("没有授权");
      });
    },
    //退出登录
    doOut(){
      const _this = this;
      this.$vux.confirm.show({
        title: '提示',
        content: '确定退出登录?',
        onCancel () {
        },
        onConfirm () {
         _this.$router.replace({ name: "login", params: { schoolNo: _this.$cookie.get(_this.$cookie.key.schoolNo) } 
          , query: {  viewType:"noWx"  } });
        }
      });
    }
  }
};
</script>

<style scoped>
@import "../../assets/scss/home.scss";
</style>

<style scoped>
.user_info_div {
  text-align: center;
    margin: 50px 20px 10px 20px;
    background:white;
    padding-bottom:20px;
      border-radius: 5px;
}
.user_info_div .user_name{
  font-size:14px;
  color:#333;
}
.user_info_div .user_detail{
  font-size:14px;
  color:#868686;
}
.user_info_div .head_big img {
  border-radius: 5px;
      position: relative;
    top: -20px;
}
 .buttom_div{
    width: 100%;
    height: 45px;
    position: absolute;
    z-index: 500;
    bottom: 90px;
    padding: 15px 20%;
    box-sizing: border-box;
  }
  .qiehuan_div{
     padding: 15px 20%;
     padding-top:40px;
  }
  #tuichu_btn{
      background:#92C5E6 !important;
  }
  .info_div{
      border-radius: 5px;
    margin: 10px 20px;
  }
  .info_div .vux-label{
     font-size:14px;
  }
  
</style>
